﻿using LD.Common;
using LD.Host;
using System;
using System.Windows.Forms;
namespace LD
{
    public partial class FrmHiveSFC1 : FrmBase
    {
        public FrmHiveSFC1()
        {
            InitializeComponent();
            UserHelper.Instance.UserChangeEvent += Instance_UserChangeEvent;
        }

        private void Instance_UserChangeEvent(UserModel user)
        {
            if (!this.IsDisposed)
            {
                this.Invoke(new Action(() =>
                {
                    if (user.UserType == UserType.操作工)
                        this.panel1.Enabled = false;
                    else
                        this.panel1.Enabled = true;
                }));
            }
        }

        private void btn_get供料器_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txt_sn.Text))
            {
                var responseStr = HttpRequestHelper.Get(this.txt_hiveUrl.Text + this.txt_sn.Text);
                if (string.IsNullOrEmpty(responseStr))
                    return;
                if (responseStr.IndexOf("SIZE") >= 0)
                {
                    bool result = false;
                    string[] strs = responseStr.Split(',');
                    //获取最后一个size字符串
                    string sizeStr = strs[strs.Length - 1];
                    if (sizeStr.IndexOf("Space Gray") >= 0)
                    {
                        Cache.SetCache(CONST.A轨_供料器, 2);
                        Cache.SetCache(CONST.B轨_供料器, 2);
                        Cache.SaveCache();

                        result = true;
                    }
                    else
                    {
                        Cache.SetCache(CONST.A轨_供料器, 1);
                        Cache.SetCache(CONST.B轨_供料器, 1);
                        Cache.SaveCache();

                        result = true;
                    }
                    if (!result)
                    {
                        MessageBox.Show("未查询到SIZE关键字后的颜色信息", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                    MessageBox.Show("返回的字符串未发现SIZE关键字", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }
        private void btn_save_Click(object sender, EventArgs e)
        {

            bool oldHiveSw = (bool)Cache.GetCache(CONST.HIVE_SW);
            bool oldSFCSw = (bool)Cache.GetCache(CONST.SFC_SW);
            string oldSfcUrl = Cache.GetCache(CONST.SFC_URL)?.ToString();
            string oldHiveUrl = Cache.GetCache(CONST.HIVE_URL)?.ToString();
            string oldMachineNo = Cache.GetCache(CONST.HIVE_MACHINENO)?.ToString();
            string oldHiveVersion = Cache.GetCache(CONST.HIVE_VERSION)?.ToString();
            string oldConfig = Cache.GetCache(CONST.Config)?.ToString();
            string oldGroup = Cache.GetCache(CONST.Group)?.ToString();
            string oldFixtureName = Cache.GetCache(CONST.FixtureName)?.ToString();
            string oldLine = Cache.GetCache(CONST.Line)?.ToString();

            if (string.IsNullOrEmpty(this.txt_hiveUrl.Text)
                || string.IsNullOrEmpty(this.txt_hiveMachineNo.Text)
                || string.IsNullOrEmpty(this.txt_hiveVersion.Text)
                || string.IsNullOrEmpty(this.txt_sfcUrl.Text)
                || string.IsNullOrEmpty(txt_Config.Text)
                || string.IsNullOrEmpty(txt_Group.Text)
                || string.IsNullOrEmpty(txt_FixtureName.Text)
                || string.IsNullOrEmpty(txt_line.Text)
           )
            {
                MessageBox.Show("配置信息不能为空");
                return;
            }
            //if (!int.TryParse(this.txt_hiveMachineNo.Text, out int machineNo))
            //{
            //    MessageBox.Show("设备编号必须为整数类型");
            //    return;
            //}
            if (!txt_Config.Text.Equals(oldConfig))
            {
                Cache.SetCache(CONST.Config, this.txt_Config.Text);
                Log4.ParamChange($"【Config】,参数【开关】,原值【{oldConfig}】修改为【{this.txt_Config.Text}】");
            }
            if (!txt_Group.Text.Equals(oldGroup))
            {
                Cache.SetCache(CONST.Group, this.txt_Group.Text);
                Log4.ParamChange($"【Group】,参数【开关】,原值【{oldGroup}】修改为【{this.txt_Group.Text}】");
            }
            if (!txt_FixtureName.Text.Equals(oldFixtureName))
            {
                Cache.SetCache(CONST.FixtureName, this.txt_FixtureName.Text);
                Log4.ParamChange($"【FixtureName】,参数【开关】,原值【{oldFixtureName}】修改为【{this.txt_FixtureName.Text}】");
            }
            if (!txt_line.Text.Equals(oldLine))
            {
                Cache.SetCache(CONST.Line, this.txt_line.Text);
                Log4.ParamChange($"【Line】,参数【开关】,原值【{oldLine}】修改为【{this.txt_line.Text}】");
            }
            if (oldHiveSw != this.chk_hive_sw.Checked)
            {
                Cache.SetCache(CONST.HIVE_SW, this.chk_hive_sw.Checked);
                Log4.ParamChange($"【HIVE】,参数【开关】,原值【{oldHiveSw}】修改为【{this.chk_hive_sw.Checked}】");
            }
            if (oldSFCSw != this.chk_sfc_sw.Checked)
            {
                Cache.SetCache(CONST.SFC_SW, this.chk_sfc_sw.Checked);
                Log4.ParamChange($"【SFC】,参数【开关】,原值【{oldSFCSw}】修改为【{this.chk_sfc_sw.Checked}】");
            }
            if (!this.txt_hiveUrl.Text.Equals(oldHiveUrl))
            {
                Cache.SetCache(CONST.HIVE_URL, this.txt_hiveUrl.Text);
                Log4.ParamChange($"【HIVE】,参数【HIVEURL】,原值【{oldHiveUrl}】修改为【{this.txt_hiveUrl.Text}】");
            }
            if (!this.txt_sfcUrl.Text.Equals(oldSfcUrl))
            {
                Cache.SetCache(CONST.SFC_URL, this.txt_sfcUrl.Text);
                Log4.ParamChange($"【SFC】,参数【SFCURL】,原值【{oldSfcUrl}】修改为【{this.txt_sfcUrl.Text}】");
            }
            if (!this.txt_hiveMachineNo.Text.Equals(oldMachineNo))
            {
                Cache.SetCache(CONST.HIVE_MACHINENO, this.txt_hiveMachineNo.Text);
                Log4.ParamChange($"【HIVE】,参数【机台编号】,原值【{oldMachineNo}】修改为【{this.txt_hiveMachineNo.Text}】");
            }
            if (!this.txt_hiveVersion.Text.Equals(oldHiveVersion))
            {
                Cache.SetCache(CONST.HIVE_VERSION, this.txt_hiveVersion.Text);
                Log4.ParamChange($"【HIVE】,参数【版本】,原值【{oldHiveVersion}】修改为【{this.txt_hiveVersion.Text}】");
            }
            Cache.SaveCache();
        }

        private void FrmMES_Shown(object sender, EventArgs e)
        {
            this.chk_hive_sw.Checked = (bool)Cache.GetCache(CONST.HIVE_SW);
            this.chk_sfc_sw.Checked = (bool)Cache.GetCache(CONST.SFC_SW);
            this.txt_sfcUrl.Text = Cache.GetCache(CONST.SFC_URL) == null ? "" : Cache.GetCache(CONST.SFC_URL).ToString();
            this.txt_hiveUrl.Text = Cache.GetCache(CONST.HIVE_URL) == null ? "" : Cache.GetCache(CONST.HIVE_URL).ToString();
            this.txt_hiveMachineNo.Text = Cache.GetCache(CONST.HIVE_MACHINENO) == null ? "" : Cache.GetCache(CONST.HIVE_MACHINENO).ToString();
            this.txt_hiveVersion.Text = Cache.GetCache(CONST.HIVE_VERSION) == null ? "" : Cache.GetCache(CONST.HIVE_VERSION).ToString();
            this.txt_Config.Text = Cache.GetCache(CONST.Config)==null?"": Cache.GetCache(CONST.Config).ToString();
            this.txt_Group.Text = Cache.GetCache(CONST.Group) == null ? "" : Cache.GetCache(CONST.Group).ToString();
            this.txt_FixtureName.Text = Cache.GetCache(CONST.FixtureName) == null ? "" : Cache.GetCache(CONST.FixtureName).ToString();
            this.txt_line.Text = Cache.GetCache(CONST.Line) == null ? "" : Cache.GetCache(CONST.Line).ToString();
        }
    }
}
